<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/7/13
 * Time: 16:44
 * Project name: PHPTEST
 * File name: 5.php
 * @copyright Copyright (c) 2016 ZRX Software LLC
 * @license https://github.com/zhangrxiang
 * @since 1.0.0
 */

function actionRegister( $type = 'email' ) {
	$registerService = new RegisterService();
	$this->layout    = 'member';
	$token           = md5( ( (float) date( "YmdHis" ) + rand( 10000000000000000, 99999999999999999 ) ) . rand( 100000, 999999 ) . "SESSION_KEY" );
	setCookie( 'token', $token, time() + 3600 );
	if ( $type === RegisterService::EMAIL ) {
		if ( isset( $_POST['member'] ) ) {
			$email = $_POST['member']['emailNumber'];
			if ( ! Yii::app()->user->isGuest ) {
				$this->redirect( 'setpwd' );
			}
			if ( $registerService->isMember( RegisterService::EMAIL, $email ) ) {
				//用户忘记密码登陆
				if ( isset( $_GET['status'] ) ) {
					Yii::app()->session['email']       = $email;
					Yii::app()->session['statuslogin'] = $_GET['statuslogin'];
					$this->redirect( 'setpwd' );
				}
				Yii::app()->user->setFlash( 'telVerify', '邮箱已被注册！' );
				$this->layout = 'member';
				$this->render( 'register' );
				exit;
			}

			Yii::app()->session['email'] = $email;
			$this->redirect( array( 'setpwd' ) );
		}
		$this->render( 'register_email' );
	} elseif ( $type === RegisterService::PHONE ) {
		if ( isset( $_POST['member'] ) ) {
			$phone = $_POST['member']['phoneNumber'];
			if ( ! Yii::app()->user->isGuest ) {
				$this->redirect( 'setpwd' );
			}
			//判断是否已注册过手机号
			if ( $registerService->isMember( 'phone', $phone ) ) {
				//用户忘记密码登陆
				if ( isset( $_GET['status'] ) ) {
					Yii::app()->session['tel']         = $phone;
					Yii::app()->session['statuslogin'] = $_GET['statuslogin'];
					$this->redirect( 'setpwd' );
				}
				Yii::app()->user->setFlash( 'telVerify', '手机号已被注册！' );
				$this->layout = 'member';
				$this->render( 'register' );
				exit;
			}
			Yii::app()->session['tel'] = $phone;
			$this->redirect( array( 'setpwd' ) );
		}
		$this->render( 'register' );
	} else {
		$this->render( 'register_email' );
	}
}